﻿namespace SdlDotNetExamples.SmallDemos

open System
open System.Collections.Generic

open SdlDotNet.Core
open SdlDotNet.Graphics

open SdlDotNet.Input

open Hainet
open Hainet.Object

module Main =

    type TestObj() =
        inherit Active(new Sprites.Sprite(new Surface("sprite.png")))        
        
        override this.OnIntersectBoundary b =
            match b with
            | Top | Bottom -> this.MotionAdd(new Speed(0.0<_>, -2.0 * this.Speed.Y))
            | Left | Right -> this.MotionAdd(new Speed(-2.0 * this.Speed.X, 0.0<_>))
        
    type TestChar() =
        inherit Controllable(new Sprites.Sprite(new Surface("sprite.png")))
        
        [<OnKey(Key.DownArrow)>]
        member this.MoveDown() =
            this.MotionAdd(new Speed(0.0<_>, 0.01<Pps>))
          
        [<OnKey(Key.UpArrow)>]  
        member this.MoveUp() =
            this.MotionAdd(new Speed(0.0<_>, -0.01<Pps>))
            
        [<OnKey(Key.RightArrow)>]  
        member this.MoveRight() =
            this.MotionAdd(new Speed(0.01<Pps>, 0.0<_>))
            
        [<OnKey(Key.LeftArrow)>]  
        member this.MoveLeft() =
            this.MotionAdd(new Speed(-0.01<Pps>, 0.0<_>))
            
        [<OnKey(Key.X)>]
        member this.Shoot() =
            ()

    type HelloWorld() =
      
        let room = new Hainet.Room.Room()
        let screen = Video.SetVideoMode(int room.Width, int room.Height)
        
        member private this.Screen = screen
            
        member this.Tick sender args =
            Video.WindowCaption <- Events.Fps.ToString()
            room.Step()
            room.OnDraw screen            

        member public this.Go() =
            Events.Quit.AddHandler(new EventHandler<QuitEventArgs>(this.Quit))
            let o = new TestChar()//Speed = new Hainet.Speed(2.0<_>, 1.0<_>))
            room.Add(o)
            let rnd = new Random()
            for x = 1 to 100 do
                let o = new TestObj()
                o.Position <- new Position(float (rnd.Next(500)) * 1.0<Pixel>,
                                           float (rnd.Next(300)) * 1.0<Pixel>)
                o.Speed <- new Speed((rnd.NextDouble() * 10.0 - 5.0) * 1.0<Pps>,
                                     (rnd.NextDouble() * 10.0 - 5.0) * 1.0<Pps>)
                room.Add(o)
            Events.Tick.AddHandler(new EventHandler<TickEventArgs>(this.Tick))
            Events.TargetFps <- 60
            Events.Run()

        member private this.Quit sender e =
            Events.QuitApplication()

        static member public Title
            with get () = "Hello World: First tutorial";
            
    [<STAThread>]
    [<EntryPoint>]
    let Main args =
        let app = new HelloWorld()
        app.Go()
        0